/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.scene.bean.ProjectPlanBean;
import com.koron.scene.bean.ProjectRealBean;
import com.koron.scene.service.api.ProjectSafetyInvestmentService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "ProjectSafetyInvest", description = "项目相关方-相关方安全投入")
@RestController
@RequestMapping("/projectSafetyInvest")
public class ProjectSafetyInvestmentController {

    private final static  Logger logger = LoggerFactory.getLogger(ProjectSafetyInvestmentController.class);

    @Autowired
    private ProjectSafetyInvestmentService service;

    @ApiOperation("列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryForList.htm", method = RequestMethod.GET)
    public MessageBean queryForList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore Integer year, @ApiIgnore String projectId,
                                    @ApiIgnore String enterpriseId, @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.queryForList(year, projectId, enterpriseId, page, pageSize);
    }

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/queryById.htm", method = RequestMethod.GET)
    public MessageBean queryById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.queryById(id);
    }

    @ApiOperation("根据ID删除相关方安全投入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    @Log(title = "根据ID删除相关方安全投入", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.delete(id);
    }

    @ApiOperation("新增相关方安全投入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryProject", value = "投入项目code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryProjectName", value = "投入项目名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryDetail", value = "投入明细" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "planTime", value = "计划投入时间(yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "planMoney", value = "计划投入金额", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personCharge", value = "责任人", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personChargeName", value = "责任人名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryDetails[i]", value = "投入明细" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realTimes[i]", value = "实际投入时间(yyyy-MM-dd HH:mm:ss)" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realityMoneys[i]", value = "实际金额" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "effectAppraisals[i]", value = "效果评估" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personCharges[i]", value = "责任人" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personChargeNames[i]", value = "责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remarks[i]", value = "备注" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "files[i]", value = "附件" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/insert.htm", method = RequestMethod.POST)
    @Log(title = "新增相关方安全投入", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean insert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                              @ApiIgnore String projectId, @ApiIgnore String enterpriseId,
                              @ApiIgnore ProjectPlanBean planBean, @ApiIgnore ProjectRealBean realBean) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.insert(user, projectId, enterpriseId, planBean, realBean);
    }

    @ApiOperation("修改相关方安全投入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相关方安全投入计划id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryProject", value = "投入项目code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryProjectName", value = "投入项目名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryDetail", value = "投入明细" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "planTime", value = "计划投入时间(yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "planMoney", value = "计划投入金额", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personCharge", value = "责任人", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personChargeName", value = "责任人名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realIds[i]", value = "相关方安全投入实际 id " , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contributoryDetails[i]", value = "投入明细" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realTimes[i]", value = "实际投入时间(yyyy-MM-dd hh:mm:ss)" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realityMoneys[i]", value = "实际金额" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "effectAppraisals[i]", value = "效果评估" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personCharges[i]", value = "责任人" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "personChargeNames[i]", value = "责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remarks[i]", value = "备注" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "accessorys[i]", value = "附件 字符串([{'filename':'xxx','fileUrl':'xxx'}])" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "previewAccessorys[i]", value = "附件 字符串([{'filename':'xxx','fileUrl':'xxx'}])" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "files[i]", value = "附件" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/update.htm", method = RequestMethod.POST)
    @Log(title = "修改相关方安全投入", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                              @ApiIgnore String projectId, @ApiIgnore String enterpriseId,
                              @ApiIgnore ProjectPlanBean planBean, @ApiIgnore ProjectRealBean realBean) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.update(user, projectId, enterpriseId, planBean, realBean);
    }

    @ApiOperation("导出相关方安全投入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份" , paramType = "query", dataType = "Integer")
    })
    @RequestMapping(value = "/export.htm", method = RequestMethod.GET)
    @Log(title = "导出相关方安全投入", businessManipulation = BusinessManipulation.EXPORT)
    public HttpEntity<?> export(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore Integer year) {
        if (null == user) {
            return null;
        }
        List<Map> list = service.exportQueryForList(year);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File file = new File("static/excel/ProjectSafetyInvestment.xlsx");
        try (
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/ProjectSafetyInvestment.xlsx");
        ) {
            FileUtils.copyInputStreamToFile(inputStream, file);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("相关方安全投入", file, "ProjectSafetyInvestment.btl", map);
    }
}
